package com.ys.tdm.controller.module;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.tdm.bean.item.Item;
import com.ys.tdm.bean.module.Module;
import com.ys.tdm.bean.user.User;
import com.ys.tdm.controller.BaseController;
import com.ys.tdm.service.item.IItemService;
import com.ys.tdm.service.module.IModuleService;
import com.ys.tdm.service.user.IUserService;
import com.ys.tdm.util.JsonUtils;
import com.ys.tdm.util.SimpleReturnVo;

@Controller
@RequestMapping("/module")
public class ModuleController extends BaseController{
	private static Logger logger = Logger.getLogger(ModuleController.class);

	@Resource 
	private IModuleService moduleService;
	@Resource 
	private IUserService userService;
	@Resource 
	private IItemService itemService;
	
	@RequestMapping("/addUI")
	public String addUI(ModelMap model, String sessionId) {
		return "module_add";
	}
	
	// 主界面
	@RequestMapping("/mainView")
	public String getAllmodule(ModelMap modelMap, Module module, HttpServletRequest request,HttpSession httpSession) {
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path;
		httpSession.setAttribute("tdmPath", basePath);
		try {
			User loginUser = this.getLoginUser(request);
			if (null != loginUser && StringUtils.isNotBlank(loginUser.getName())) {
				modelMap.addAttribute("userInfor", loginUser);
			}
			List<Module> allModule = moduleService.getAll(module);
			if (!allModule.isEmpty()) {
				modelMap.addAttribute("allModule", allModule);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "main_view";
	}

	// 添加模块
	@ResponseBody
	@RequestMapping("/add")
	public String add(Module module,HttpServletRequest request) {
		SimpleReturnVo returnVo = new SimpleReturnVo(ERROR, "添加失败");
		try {
			User loginUser = this.getLoginUser(request);
			if (null != loginUser && StringUtils.isNotBlank(loginUser.getName())) {
				String userName = loginUser.getName();
				module.setCreator(userName);
				module.setUpdator(userName);
				this.moduleService.insertModule(module);
				returnVo = new SimpleReturnVo(SUCCESS, "添加成功");
			} else {
				returnVo = new SimpleReturnVo(ERROR, "用户信息获取失败，请重新登录");
			}
		} catch (Exception e) {
			logger.error("ItemController-add:", e);
			e.printStackTrace();
		}
		return JsonUtils.toJson(returnVo);
	}
	
	// 删除
	@ResponseBody
	@RequestMapping("/del")
	public String del(String id, HttpServletRequest request) {
		SimpleReturnVo returnVo = new SimpleReturnVo(ERROR, "删除失败");
		try {
			if (StringUtils.isNotBlank(id)) {
				Item item = new Item();
				item.setModuleId(id);
				List<Item> all = itemService.getAll(item);
				if (!all.isEmpty()) {
					returnVo.setResponseCode(UNPREMISSION);
					returnVo.setResponseMsg("不允许的操作");
				} else {
					this.moduleService.delModuleById(id);
					returnVo = new SimpleReturnVo(SUCCESS, "删除成功");
				}
			}
		} catch (Exception e) {
			logger.error("ModuleController-del:", e);
			e.printStackTrace();
		}
		return JsonUtils.toJson(returnVo);

	}
	
	

}
